package com.hy.study.mapper;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 切片按指定的行切片
 *
 * @author huyong
 * @date 2024 年 01 月 31 日 10:12: 17
 */
public class NLineWordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
    private final LongWritable v = new LongWritable(1);
    private final Text k = new Text();

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        String line = value.toString();
        if (StringUtils.isNotBlank(line)) {
            String[] words = line.split(" ");
            for (String word : words) {
                k.set(word);
                context.write(k, v);
            }
        }
    }
}
